package com.szxy.item.service.impl;

import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.netflix.discovery.converters.Auto;
import com.szxy.item.service.ItemService;
import com.szxy.mapper.TbItemMapper;
import com.szxy.pojo.TbItem;
import com.szxy.pojo.TbItemExample;
import com.szxy.utils.PageResult;
import com.szxy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Auther:zwer
 * @Date:2019/9/7 14:20
 * @Description:com.szxy.item.service.impl
 * @Version:1.0
 **/
@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private TbItemMapper itemMapper;

    @Override
    public PageResult selectTbItemAllByPage(Integer page, Integer rows) {
        PageHelper.startPage(page,rows); //Mybatis 分页插件
        TbItemExample example = new TbItemExample();
        TbItemExample.Criteria criteria = example.createCriteria();
        //where status = ?
        criteria.andStatusEqualTo((byte)1);
        List<TbItem> tbItems = this.itemMapper.selectByExample(example);
        PageInfo<TbItem> itemPageInfo = new PageInfo<>(tbItems);
        PageResult pageResult = new PageResult();
        pageResult.setPageIndex(itemPageInfo.getPageNum());
        pageResult.setTotalPage(itemPageInfo.getSize());
        pageResult.setResult(itemPageInfo.getList());
        return pageResult;
    }

    @Override
    @LcnTransaction
    public Integer insertTbItem(TbItem item) {
            return this.itemMapper.insert(item);
    }

    @Override
    @LcnTransaction
    public Integer updateItemById(TbItem item) {
        item.setUpdated(new Date());
        return this.itemMapper.updateByPrimaryKeySelective(item);
    }

    @Override
    public TbItem findItemById(Long itemId) {
        return this.itemMapper.selectByPrimaryKey(itemId);
    }

}
